package com.sunhao.application;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.sunhao.application.util.ViewUtil;

import java.util.Random;

public class LoginForgetActivity8 extends AppCompatActivity {

    String mverifyCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_forget8);
        EditText password_new = findViewById(R.id.password_new);
        EditText password_confirm = findViewById(R.id.password_confirm);
        EditText verifyCode = findViewById(R.id.verifyCode);
        Button getverifyCode = findViewById(R.id.getverifyCode);
        Button ok = findViewById(R.id.ok);


        //新密码和确认密码隐藏键盘 6位
        password_new.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 6) {
                    ViewUtil.hideOneInputMethod(LoginForgetActivity8.this, password_new);
                }
            }
        });
        password_confirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 6) {
                    ViewUtil.hideOneInputMethod(LoginForgetActivity8.this, password_confirm);
                }
            }
        });
        verifyCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 6) {
                    ViewUtil.hideOneInputMethod(LoginForgetActivity8.this, password_confirm);
                }
            }
        });


        //从上一个页面获取要修改的手机号码
        String phone = getIntent().getStringExtra("phone");

        //获取验证码点击事件
        getverifyCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //首先判断是否相同
                if (!password_new.getText().toString().equals(password_confirm.getText().toString())) {
                    //不相同,提示
                    Toast.makeText(LoginForgetActivity8.this, "两次输入密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                }

                //生成验证码
                mverifyCode = String.format("%6d", new Random().nextInt(999999));

                //验证码弹窗
                AlertDialog.Builder builder = new AlertDialog.Builder(LoginForgetActivity8.this);
                builder.setTitle("请记住验证码");
                builder.setMessage(String.format("您的手机号是%s,本次验证码是%s,请输入验证码", phone, mverifyCode));
                builder.setPositiveButton("好的", null);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });


        //登陆点击
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进行验证码校验
                if (!verifyCode.getText().toString().equals(mverifyCode)) {
                    //弹出校验不正确
                    Toast.makeText(LoginForgetActivity8.this, "验证码不正确,请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                }
                //保存密码,传给上一个页面
                Intent intent = new Intent();
//                Bundle bundle = new Bundle();
//                bundle.putString("password", password_confirm.getText().toString());
//                intent.putExtras(bundle);
                intent.putExtra("password", password_confirm.getText().toString());
                setResult(1, intent);
                finish();
            }
        });

    }
}